#include <bits/stdc++.h>
using namespace std;

#define int long long

int t, n, a[200471];
signed main() {
	freopen("color.in", "r", stdin);
	freopen("color.out", "w", stdout);
	scanf("%lld", &t);
	while (t--) {
		int mx = 0, tag = 0;
		unordered_map<int, int> M;
		M[0] = 0;
		scanf("%lld", &n);
		for (int i = 1; i <= n; i++) {
			scanf("%lld", &a[i]);
			if (a[i] == a[i - 1]) {
				tag += a[i];
			} else {
				M[a[i - 1]] = mx;
				if (M.count(a[i])) {
					M[a[i - 1]] = max(M[a[i - 1]], M[a[i]] + a[i]);
					mx = max(mx, M[a[i - 1]]);
				}
			}
		}
		printf("%lld\n", tag + mx);
	}
}
